package com.leo.mapper;

import com.leo.bean.Customer;
import com.leo.bean.EmailInfo;
import com.leo.bean.MessageInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 使用注解的方式
 */
public interface UserMapper {
    /**
     * 根据id查询客户信息
     *
     * @param email
     * @return
     */
    @Select("select id, username, email from email_user where email = #{email} and password = #{password}")
    Customer selectCustomer(@Param("email") String email, @Param("password") String password);

    @Select("select id, username, email from email_user where email = #{email}")
    Customer selectCustomerByEmail(@Param("email") String email);

    @Select("select count(1) from email_user where email = #{email} and username = #{username}")
    int selectCount(@Param("username") String username, @Param("email") String email);

    @Select("select count(1) from email_user where email = #{email}")
    int selectCountByEmail(@Param("email") String email);

    @Update("update email_user set password = #{password} where email = #{email} and username = #{username}")
    int updatePassword(@Param("username") String username, @Param("email") String email, @Param("password") String password);

    @Insert("insert into email_user(username, email, password) VALUES(#{username}, #{email}, #{password})")
    int insertCustomer(@Param("username") String username, @Param("email") String email, @Param("password") String password);

    @Select("select id, type, email, password, showName, sendName,receiveServer,receivePort ,sendServer ,sendPort, intervalTime, status, createTime, updateTime, email_user_id, emailCount  " +
            "from email_info where email_user_id = #{userId} and status = 0 order by createTime")
    List<EmailInfo> selectByUserId(int userId);

    @Insert("insert into email_info(type, email, password, showName, sendName, receiveServer, receivePort, sendServer, sendPort, intervalTime, status, createTime, updateTime, email_user_id)" +
            " values (#{info.type}, #{info.email},#{info.password},#{info.showName},#{info.sendName},#{info.receiveServer},#{info.receivePort},#{info.sendServer}," +
            "#{info.sendPort},#{info.intervalTime},#{info.status},#{info.createTime},#{info.updateTime},#{info.email_user_id})")
    @Options(useGeneratedKeys = true, keyProperty = "info.id", keyColumn = "id")
    int insertMailInfo(@Param("info") EmailInfo info);

    @Update("update email_info set emailCount = emailCount + #{count} where id = #{id}")
    int updateMailInfoCount(@Param("count") int count, @Param("id") int id);

    @Select("select emailCount from email_info where id = #{id} and status = 0")
    int selectMailInfoCount(int id);

    @Insert("insert into email_message_info(subject, emailFrom, emailReceive, emailCc, sendTime, emailSize, isAttachment, attachmentPath, contentPath, emailPriority, isReceipt, createTime, updateTime, email_info_id, status)" +
            " values (#{info.subject},#{info.emailFrom},#{info.emailReceive},#{info.emailCc},#{info.sendTime},#{info.emailSize},#{info.isAttachment},#{info.attachmentPath},#{info.contentPath},#{info.emailPriority},#{info.isReceipt},#{info.createTime},#{info.updateTime},#{info.email_info_id},#{info.status})")
    @Options(useGeneratedKeys = true, keyProperty = "info.id", keyColumn = "id")
    int insertMessageInfo(@Param("info") MessageInfo info);

    @Update("update email_message_info set subject=#{info.subject},emailReceive=#{info.emailReceive},emailCc=#{info.emailCc},sendTime=#{info.sendTime},emailSize=#{info.emailSize},isAttachment=#{info.isAttachment},attachmentPath=#{info.attachmentPath},contentPath=#{info.contentPath},emailPriority=#{info.emailPriority},isReceipt=#{info.isReceipt},updateTime=#{info.updateTime},email_info_id=#{info.email_info_id},status=#{info.status} where id = #{info.id}")
    int updateMessageInfo(@Param("info") MessageInfo info);

    @Update("update email_message_info set status=#{status} where id = #{id}")
    int updateMessageInfoStatus(@Param("status") int status, @Param("id") int id);

    @Delete("delete from email_message_info where id = #{id}")
    int delMessageInfo(int id);

    // TODO 这里有问题
    @Select("(select t.* from email_message_info t where email_info_id = #{mailId} order by sendTime)")
    List<MessageInfo> selectMessageInfoByMailId(int mailId);

    @Select("select t.emailFriend from email_friend_info t")
    List<String> selectFriendInfos();

    @Insert("insert into email_friend_info(emailFriend) values (#{email})")
    int insertFriendInfo(String email);

    @Delete("delete from email_friend_info where emailFriend = #{email}")
    int deleteFriendInfo(String email);

    @Update("update email_friend_info set emailFriend = #{newMail} where emailFriend = #{oldMail}")
    int updateFriendInfo(@Param("newMail") String newMail, @Param("oldMail") String oldMail);
}
